package UI;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import thesis.FSModule;
import thesis.InternTweetInfoExtractor;
import thesis.MoreInfoStoredFrame;
import thesis.StatsInfoExtractor;


import com.mongodb.DBObject;

public class VisualizationStoredFrame extends JFrame implements ActionListener {
	private static final long serialVersionUID = 1L;

	private int summaryDimension;
	private List<DBObject> tweets;
	private int currentPage;
	private int numberOfPages;
	
	private JButton infoButtons[] = null;
	private JLabel tweetTexts[] = null;
	private JButton next;
	private JButton previous;
	
	public VisualizationStoredFrame(int idStoredSummary){ 
		super("Results");
		
		DBObject stats = FSModule.readStoredStatistics(idStoredSummary);
		StatsInfoExtractor statsInfoExtr = new StatsInfoExtractor(stats);
		
		tweets = FSModule.readStoredSummary(idStoredSummary);
		summaryDimension = statsInfoExtr.getSummarySize();
		currentPage = 1;
		numberOfPages = statsInfoExtr.getNumberOfPages();
		
		setBounds(0,0, GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width, GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		Container c = this.getContentPane();
		c.setLayout(new GridLayout(3 + summaryDimension, 1));
		
		JPanel panelTitle = new JPanel();
		panelTitle.setLayout(new GridLayout(1,1));
		JLabel title = new JLabel("FILTERING AND SUMMARIZATION MODULE RESULTS");
		Font fontTitle = new Font("Arial", Font.BOLD, 40);
		title.setFont(fontTitle);
		title.setHorizontalAlignment(JLabel.CENTER);
		panelTitle.add(title);
		
		c.add(panelTitle);
		
		JPanel panelStatistics = new JPanel();
		panelStatistics.setLayout(new GridLayout(2,3));
		
		String beginDateString = statsInfoExtr.getBeginDate();
		JLabel beginDate = new JLabel("Begin date: " + beginDateString);
		Font fontStats = new Font("Arial", Font.PLAIN, 25);
		beginDate.setFont(fontStats);
		panelStatistics.add(beginDate);
		
		if(statsInfoExtr.hasEndDate()){
			String endDateString =  statsInfoExtr.getEndDate();
			JLabel endDate = new JLabel("End date: " + endDateString);
			endDate.setFont(fontStats);
			panelStatistics.add(endDate);
		}
		else{
			JLabel tweetsNumberLimit = new JLabel("Tweets number limit: " + statsInfoExtr.getTweetsNumberLimit());
			tweetsNumberLimit.setFont(fontStats);
			panelStatistics.add(tweetsNumberLimit);
		}
		
		JLabel summaryDim = new JLabel("Summary dimension: " + summaryDimension);
		summaryDim.setFont(fontStats);
		panelStatistics.add(summaryDim);
		
		JLabel quality = new JLabel("Quality: " + statsInfoExtr.getQuality());
		quality.setFont(fontStats);
		panelStatistics.add(quality);
		
		JLabel coverage = new JLabel("Coverage: " + statsInfoExtr.getCoverage());
		coverage.setFont(fontStats);
		panelStatistics.add(coverage);
		
		JLabel diversity = new JLabel("Diversity: " + statsInfoExtr.getDiversity());
		diversity.setFont(fontStats);
		panelStatistics.add(diversity);
		
		c.add(panelStatistics);

		infoButtons = new JButton[summaryDimension];
		tweetTexts = new JLabel[summaryDimension];
		
		for (int i=0; i<summaryDimension; i++){
			JPanel panelTweet = new JPanel();
			panelTweet.setLayout(new GridLayout(2,1));
			
			DBObject tweet = tweets.get(i);
			InternTweetInfoExtractor infoExtr = new InternTweetInfoExtractor(tweet);
			
			tweetTexts[i] = new JLabel(infoExtr.getText());
			Font fontTweets = new Font("Times New Roman", Font.PLAIN, 20);
			tweetTexts[i].setFont(fontTweets);
			
			JPanel panelButton = new JPanel();
			infoButtons[i] = new JButton("more info");
			infoButtons[i].addActionListener(this);
			
			if(i % 2 == 0){
				panelTweet.setBackground(Color.white);
				panelButton.setBackground(Color.white);
			}
			else{
				panelTweet.setBackground(Color.lightGray);
				panelButton.setBackground(Color.lightGray);
			}
			
			
			panelTweet.add(tweetTexts[i]);
			panelButton.add(infoButtons[i]);
			panelTweet.add(panelButton);
			
			c.add(panelTweet);
		}
		
		JPanel panelChangeResults = new JPanel();
		panelChangeResults.setLayout(new GridLayout(2,2));
		panelChangeResults.add(new JPanel());
		panelChangeResults.add(new JPanel());
		
		JPanel panelNext = new JPanel();
		next = new JButton("next");
		next.addActionListener(this);
		if(numberOfPages == 1){
			next.setEnabled(false);
		}
		
		panelNext.add(next);
		
		JPanel panelPrevious = new JPanel();
		previous = new JButton("previous");
		previous.addActionListener(this);
		previous.setEnabled(false);
		
		panelPrevious.add(previous);
		
		panelChangeResults.add(panelPrevious);
		panelChangeResults.add(panelNext);
		
		c.add(panelChangeResults);
		
	}

	public void actionPerformed(ActionEvent e) {
		for (int i=0; i<summaryDimension; i++){
			if (infoButtons[i] == e.getSource()){
				DBObject tweet = tweets.get(i + (currentPage-1)*summaryDimension);
				
				MoreInfoStoredFrame infoFrame = new MoreInfoStoredFrame(tweet);
				infoFrame.setVisible(true);
			}
		}
		
		if(e.getSource() == next){
			currentPage++;
			previous.setEnabled(true);
			
			if (currentPage == numberOfPages){
				next.setEnabled(false);
			}
			
			for (int i=0; i<summaryDimension; i++){
				DBObject tweet = tweets.get(i + (currentPage-1)*summaryDimension);
				InternTweetInfoExtractor infoExtr = new InternTweetInfoExtractor(tweet);
				
				tweetTexts[i].setText(infoExtr.getText());
			}
		}
		
		if(e.getSource() == previous){
			currentPage--;
			next.setEnabled(true);
			
			if (currentPage == 1){
				previous.setEnabled(false);
			}
			
			for (int i=0; i<summaryDimension; i++){
				DBObject tweet = tweets.get(i + (currentPage-1)*summaryDimension);
				InternTweetInfoExtractor infoExtr = new InternTweetInfoExtractor(tweet);
				
				tweetTexts[i].setText(infoExtr.getText());
			}
		}
		
	}
}
